package com.cuitrip.component.item;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Adapter;
import android.widget.ListView;

import com.cuitrip.business.user.Indexer;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.utils.Utils;

/**
 * Created by sheena on 16/3/11.
 */
public class CustomIndexBar extends View {

    private Paint mPaint;
    private String indexText;
    private int indexTextColor;
    private float indexTextSize;
    private Rect mBounds;
    private int cellHight;//单个字母高度
    private int cellWidth;//单个字母宽度
    private Context context;
    private int mChoose = -1;//未选中任何字母
    private ListView mListView;
    private RecyclerView mRecyclerView;
    private Indexer indexer;


    public CustomIndexBar(Context context) {
        this(context, null);
    }

    public CustomIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomIndexBar, defStyleAttr, 0);
        indexText = a.getString(R.styleable.CustomIndexBar_indexText);
        indexTextSize = a.getDimension(R.styleable.CustomIndexBar_indexTextSize, 17.f);
        indexTextColor = a.getColor(R.styleable.CustomIndexBar_indexTextColor, Color.BLACK);
        a.recycle();
        init();
    }


    private void init() {
        mPaint = new Paint();
        mPaint.setTextSize(indexTextSize);
        mPaint.setColor(indexTextColor);
        mPaint.setAntiAlias(true);
        mBounds = new Rect();
        if (indexText == null) {
            indexText = "";
        }
        mPaint.getTextBounds(indexText, 0, indexText.length(), mBounds);
        cellHight = (int) (indexTextSize + Utils.dp2pixel(3));
        cellWidth = (int) (indexTextSize + Utils.dp2pixel(3));
    }

    public void setData(String text, ListView listView){
        mListView = listView;
        Adapter adapter = mListView.getAdapter();
        if (adapter == null || !(adapter instanceof Indexer)) throw new RuntimeException("ListView must set Adapter or Adapter must implements Indexer interface");
        indexer = (Indexer) adapter;
        indexText = text;
        requestLayout();
    }




    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = cellWidth + getPaddingRight() + getPaddingLeft();
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = cellHight * indexText.length() + getPaddingTop() + getPaddingBottom();
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        for (int i = 0; i < indexText.length(); i++) {
            if (i == mChoose) {
                mPaint.setColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
            } else {
                mPaint.setColor(ResourceUtil.getThemeColor());
            }
            char c = indexText.charAt(i);
            String s = String.valueOf(c);
            canvas.drawText(s, getMeasuredWidth() / 2 - mPaint.measureText(s) / 2, cellHight / 2 + i * cellHight + mPaint.measureText(s) / 2, mPaint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index=-1;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int startY = (int) event.getY();
                index = startY / cellHight;
                if (index >= indexText.length()) {
                    index = indexText.length() - 1;
                } else if (index < 0) {
                    index = 0;
                }
                mChoose = index;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP: {
                mChoose = -1;
            }

            break;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            if (indexer!=null) {
                int position = indexer.getStartPositionOfSection(String.valueOf(indexText.charAt(index)));
                if (position == -1) {
                    return true;
                }

                mListView.setSelection(position);
            }
        }
        invalidate();
        return true;
    }


}
